<?php

use Medoo\Medoo;

class OrderService extends TP_Service {
    private $role;
    private $redis;
    private $client;

    public function __construct() {
        parent::__construct();
        $this->load->model("SModel/Order_model");
        $this->load->model("SModel/Order_goods_model");
        $this->load->model("SModel/Pattern_model");
        $this->load->model("SModel/Customer_model");
        $this->load->model("SModel/Staff_model");
        $this->load->model("SModel/Account_model");


        $this->role = $this->getRole($_SESSION[SESS_UUID]);
        $this->redis = $this->newRedis();
        // $this->client = new Yar_Client('http://patternrpc.shaohuajj.com/server.php');
        $this->client = new Yar_Client($this->config->item('rpc')['url']);
    }




    /**
     * 方法名 orderList
     *
     * @Name 获取方法列和角色方法列
     * @Desc 订单列表
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     */
    public function orderList($param, $export = false) {
        if (!$export) {
            $param = array_filter($param, 'filtrfunction');
            if (!isset($param['pageNo'], $param['pageSize'])) {
                exit(retJson(ErrCode::ErrCode_10002));
            }

            $conarr['pageNo'] = $param['pageNo'];
            $conarr['pageSize'] = $param['pageSize'];
        }


        // 1:个人订单 2:审核订单
        if (!isset($param['listType']) || !in_array($param['listType'], [1, 2])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }


        // $conarr['join'] = array("tp_order p", 'tp_order.pid = p.pid');
        $conarr['items'] = "id, no, num, salesman, cstmr_num, cstmr_name, created, update_info,status,abolish";
        $conarr['orderby'] = "created desc";
        if ($param['listType'] == 1) {
            $conarr['where'] = sql_format("creator = '{0}'", $_SESSION[SESS_UUID]);
        } else {
            if ($this->role->role_id == WebRole::Salesman[TPEnum::Value]) {
                $conarr['where'] = sql_format("salesman_uuid = '{0}' and creator != '{0}' and  status >= '{1}'", $_SESSION[SESS_UUID], SubmitStatus::First[TPEnum::Value]);
            } elseif($this->role->role_id == WebRole::BusinessS[TPEnum::Value]) {
                $con['join'] = array("tp_account a","a.id = tp_staff.aid");
                $con['items'] = "a.uuid";
                $con['where'] = sql_format("ascription = '{0}'",$this->role->id);
                $sale = $this->Staff_model->get_list_full($con);

                $arr = [];
                $arr[] = $_SESSION[SESS_UUID];
                foreach( $sale as $K => $v){
                    $arr[] = $v['uuid'];
                }
                $sales = implode("','",$arr);
                $conarr['where'] = sql_format("salesman_uuid in ('{0}') and status >= '{1}' ",  $sales, SubmitStatus::second[TPEnum::Value]);
            }elseif($this->role->role_id == WebRole::manager[TPEnum::Value]) {
                $con['join'] = array("tp_account a","a.id = tp_staff.aid");
                $con['items'] = "a.uuid";
                $con['where'] = sql_format("manager = '{0}'",$this->role->id);
                $sale = $this->Staff_model->get_list_full($con);
                $arr = [];
                $arr[] = $_SESSION[SESS_UUID];
                foreach( $sale as $K => $v){
                    $arr[] = $v['uuid'];
                }
                $sales = implode("','",$arr);
                $conarr['where'] = sql_format("salesman_uuid in ('{0}') and status >= '{1}' ",  $sales, SubmitStatus::second[TPEnum::Value]);
            }else{
                $conarr['where'] = "1=1";
            }
        }


        // // 搜索
        if (isset($param['status']) && $param['status'] != "") {
            if ($param['status'] == 0 || $param['listType'] == 2) {

                if($this->role->role_id == WebRole::Manage[TPEnum::Value] && $param['status'] != 0){
                    $conarr['where'] .= sql_format(" AND status >= '{0}' AND abolish = 0", $param['status']);

                }else{
                    $conarr['where'] .= sql_format(" AND status = '{0}'  AND abolish = 0", $param['status']);

                }
            } else {
                $conarr['where'] .= sql_format(" AND status >= '{0}'", $param['status']);
            }
        }
        if (isset($param['abolish']) && $param['abolish'] != "") {

            $conarr['where'] .= sql_format(" AND abolish = '{0}'", $param['abolish']);
        }
        if (!empty($param['no'])) {
            $conarr['where'] .= sql_format(" AND no = '{0}'", $param['no']);
        }
        if (!empty($param['salesman'])) {
            $conarr['where'] .= sql_format(" AND salesman LIKE '{0}%'", $param['salesman']);
        }
        if (!empty($param['cstmr_num'])) {
            $conarr['where'] .= sql_format(" AND cstmr_num = '{0}'", $param['cstmr_num']);
        }
        if (!empty($param['cstmr_name'])) {
            $conarr['where'] .= sql_format(" AND cstmr_name LIKE '{0}%'", $param['cstmr_name']);
        }
        // if (!empty($param['num1'])) {
        //     $conarr['where'] .= sql_format(" AND num >= '{0}'", $param['num1']);
        // }
        // if (!empty($param['num2'])) {
        //     $conarr['where'] .= sql_format(" AND num <= '{0}'", $param['num2']);
        // }
        if (!empty($param['created']) && is_array($param['created'])) {
            $conarr['where'] .= sql_format(" AND created BETWEEN '{0}' AND '{1}'", $param['created'][0], $param['created'][1]);
        }


        if ($export) {
            $list = $this->Order_model->get_list_full($conarr);
        } else {
            $list = $this->Order_model->get_page_list($conarr);
            foreach($list['ret'] as $k => &$v){
                $v['status_char'] = (new SubmitStatus)->show($v['status']);
            }
        }
        return $list;
    }



    // 能否操作
    private function hasPre() {
        if ($this->role->role_id != WebRole::Manage[TPEnum::Value]) {
            exit(retJson(ErrCode::ErrCode_10404));
        }
    }


    /**
     * 方法名 orderDelete
     *
     * @Name 获取方法列和角色方法列
     * @Desc 订单删除
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     */
    public function orderDelete($param) {
        if (!isset($param['oid']) || empty($param['oid'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }


        $this->Order_model->delete(['id' => $param['oid']]);


        // 花型库存增加
        $allPattern = $this->Order_goods_model->get_list_full(['items' => 'pid, num', 'where' => sql_format(" oid = '{0}'", $param['oid'])]);
        foreach ($allPattern as $value) {
            $this->dealStock($value['pid'], $value['num']);
        }


        $this->Order_goods_model->delete(['oid' => $param['oid']]);
        return true;
    }



    /**
     * 方法名 orderPatDelete
     *
     * @Name 获取方法列和角色方法列
     * @Desc 订单花型删除
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     */
    public function orderPatDelete($param) {
        if (!isset($param['oid'], $param['pid']) || empty($param['oid']) || empty($param['pid'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }

        // 花型库存增加
        // $info = $this->Order_goods_model->get_single(['oid' => $param['oid'], 'pid' => $param['pid']]);
        // $this->dealStock($param['pid'], $info['num']);


        $this->Order_goods_model->delete(['oid' => $param['oid'], 'pid' => $param['pid']]);


        // 订单花型数量-1
        $sql = sql_format("UPDATE tp_order SET num = num - 1 WHERE id = '{0}' AND is_delete = 0", $param['oid']);
        $this->Order_model->execute($sql);
        return true;
    }



    /**
     * 方法名 orderInfo
     *
     * @Name 获取方法列和角色方法列
     * @Desc 订单详情
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     */
    public function orderInfo($param) {
        $param = array_filter($param);
        if (!isset($param['pageNo'], $param['pageSize'], $param['oid'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }


        $conarr = array(
            'pageNo'   => $param['pageNo'],
            'pageSize' => $param['pageSize'],
            'items'    => "id,color,p.picture_thumb,oid, p.pid, num, delivery_date, remark, no, unit, stock, series",
            'join'     => array("tp_pattern p", 'tp_order_goods.pid = p.pid'),
            'where'    => sql_format("oid = '{0}'", $param['oid']),
            'orderby'  => 'id desc',
        );


        if (!empty($param['no'])) {
            $conarr['where'] .= sql_format(" AND no = '{0}'", $param['no']);
        }
        if (!empty($param['unit'])) {
            $conarr['where'] .= sql_format(" AND unit = '{0}'", $param['unit']);
        }
        if (!empty($param['num1'])) {
            $conarr['where'] .= sql_format(" AND num >= '{0}'", $param['num1']);
        }
        if (!empty($param['num2'])) {
            $conarr['where'] .= sql_format(" AND num <= '{0}'", $param['num2']);
        }
        if (!empty($param['delivery_date']) && is_array($param['delivery_date'])) {
            $conarr['where'] .= sql_format(" AND delivery_date BETWEEN '{0}' AND '{1}'", $param['delivery_date'][0], $param['delivery_date'][1]);
        }

        if (!empty($param['series'])) {
            $conarr['where'] .= sql_format(" AND series = '{0}'", $param['series']);
        }
        if (!empty($param['stock'])) {
            $conarr['where'] .= sql_format(" AND stock = '{0}'", $param['stock']);
        }
        // if (!empty($param['region'])) {
        //     $conarr['where'] .= sql_format(" AND region = '{0}'", $param['region']);
        // }
        // if (!empty($param['country'])) {
        //     $conarr['where'] .= sql_format(" AND country = '{0}'", $param['country']);
        // }
        // if (!empty($param['sealing_time']) && is_array($param['sealing_time'])) {
        //     $conarr['where'] .= sql_format(" AND sealing_time BETWEEN '{0}' AND '{1}'", $param['sealing_time'][0], $param['sealing_time'][1]);
        // }


        $list = $this->Order_goods_model->get_page_list($conarr);
        return $list;
    }




    /**
     * 方法名 orderExport
     *
     * @Name 订单导出
     * @Desc orderExport
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2022-01-21 16:26:36
     * @return      [type]                   [description]
     */
    public function orderExport($param) {
        $list = $this->orderList($param, true);

        $this->load->library('ProjectService/Xlsxwriter');
        $writer = new XLSXWriter();

        if (empty($list)) {
            exit(retJson(ErrCode::ErrCode_10902));
        }



        if ($this->role->role_id == WebRole::Customer[TPEnum::Value]) {
            $end_col = 10;
            $title = ['订单号', '下单花型数量', '业务员', '客户号', '客户名', '添加时间', '花型编号', '花型单位', '下单数量', '要求交期', '备注'];
        } else {
            $end_col = 12;
            $title = ['订单号', '下单花型数量', '业务员', '客户号', '客户名', '添加时间', '花型编号', '花型系列', '花型单位', '下单数量', '要求交期', '库存数量', '备注'];
        }

        $writer->writeSheetRow('sheet1', ['订单导出'], ['valign' => 'center', 'halign' => 'center', 'height' => 55, 'font' => '宋体', 'font-size' => 16, 'font-style' => 'bold']);
        $writer->markMergedCell('sheet1', $start_row = 0, $start_col = 0, $end_row = 0, $end_col = $end_col);
        // $title = ['订单号', '下单数量', '业务员', '客户号', '客户名', '添加时间'];
        $styles = ['font' => '宋体', 'font-size' => 10, 'halign' => 'center', 'border' => 'left,right,top,bottom', 'valign' => "center", 'border-style' => "thin", 'height' => 50, 'font-style' => 'bold'];
        $writer->writeSheetRow('sheet1', $title, $styles);



        // 写入内容
        unset($styles['font-style']);
        $styles['height'] = 30;
        $startRow = 0;
        $endRow = 1;
        foreach ($list as $v) {
            // 获取订单花型信息
            $conarr = array(
                'items'    => "num, delivery_date, remark, no, unit, stock, series",
                'join'     => array("tp_pattern p", 'tp_order_goods.pid = p.pid'),
                'where'    => sql_format("oid = '{0}'", $v['id']),
            );
            $patterns = $this->Order_goods_model->get_list_full($conarr);

            foreach ($patterns as $val) {
                if ($this->role->role_id == WebRole::Customer[TPEnum::Value]) {
                    $item = [
                        $v['no'], $v['num'], $v['salesman'], $v['cstmr_num'], $v['cstmr_name'], $v['created'],
                        $val['no'], $val['unit'], $val['num'], $val['delivery_date'], $val['remark'],
                    ];
                } else {
                    $item = [
                        $v['no'], $v['num'], $v['salesman'], $v['cstmr_num'], $v['cstmr_name'], $v['created'],
                        $val['no'], $val['series'], $val['unit'], $val['num'], $val['delivery_date'],
                        $val['stock'], $val['remark'],
                    ];
                }

                $writer->writeSheetRow('sheet1', $item, $styles);
            }

            $startRow = $endRow + 1;
            $endRow = $startRow + count($patterns) - 1;
            // 上下合并0~5单元格
            for ($i = 0; $i <= 5; $i++) {
                $writer->markMergedCell('sheet1', $start_row = $startRow, $start_col = $i, $end_row = $endRow, $end_col = $i);
            }
        }


        $path = 'export/order/';
        if (!file_exists($path)) {
            mkdir($path, 0777);
        }
        $file = $path . time() . '.xlsx';
        $writer->writeToFile($file);
        return array('path' => $file);
    }




    /**
     * 方法名 orderAdd
     *
     * @Name 获取方法列和角色方法列
     * @Desc 订单录入
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     *
     */
    public function orderAdd($param) {
        if (!isset($param['delivery_date'], $param['goods']) || !is_array($param['goods'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }
        // $this->db->trans_start();

        // 库存是否足够
        // foreach ($param['goods'] as $value) {
        //     // $stock = $this->getStock($value['pid']);
        //     $sql = sql_format("SELECT stock FROM tp_pattern WHERE pid = {0} FOR UPDATE", $value['pid']);
        //     $info = $this->Pattern_model->single($sql);
        //     if ($info->stock < $value['num']) {
        //         $this->db->trans_complete();
        //         exit(retJson(ErrCode::ErrCode_10903));
        //     }
        // }

		$total = 0;
		foreach ($param['goods'] as $item) {
			$total += $item['num'];
		}

        $date = date('YmdHis');
        $order = [
            'no'            => $date,
            'num'           => $total,
            'created'       => $date
        ];
        if (empty($param['cstmr_num']) && empty($param['cstmr_name'])) {
            // 客户下单
            $cust = $this->Customer_model->get_single(['aid' => $this->role->id]);

            $ascription = $this->Account_model->get_single(['id' => $cust['ascription']]);
            $order['cstmr_num']     = $this->role->usercode;
            $order['cstmr_name']    = $this->role->username;
            $order['salesman']      = $cust['ascription_name'];
            $order['salesman_uuid'] = $ascription['uuid'];
        } else {
            // 非客户下单
            $order['cstmr_num']     = $param['cstmr_num'];
            $order['cstmr_name']    = $param['cstmr_name'];
            $order['salesman']      = $this->role->username;
            $order['salesman_uuid'] = $this->role->uuid;
        }
        $oid = $this->Order_model->add($order);

        // 订单商品
        foreach ($param['goods'] as $value) {
            $goods[] = [
                'oid'           => $oid,
                'pid'           => $value['pid'],
                'num'           => $value['num'],
                'delivery_date' => $param['delivery_date'],
                'created'       => $date,
				'color'     	=> isset($value['color']) ? $value['color'] : ''
            ];

            // 花型库存减少
            // $this->dealStock($value['pid'], $value['num'], true);
        }

        $this->Order_goods_model->add($goods);

        // 购物车清空
        $this->redis->del("pattern:shopcat:". $this->role->id);

		// 购物车清空
		$this->redis->del("pattern:cart:". $this->role->id);


        // $this->db->trans_complete();

        return true;
    }


    /**
     * 方法名 dealStock
     *
     * @Name dealStock
     * @Desc 处理库存
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2022-02-17 14:11:28
     * @return      [type]                   [description]
     */
    private function dealStock($pid, $stock, $type = false) {
        $sql = sql_format("UPDATE tp_pattern SET stock = stock + '{0}' WHERE pid = '{1}' AND is_delete = 0", $stock, $pid);
        if ($type) {
            $sql = sql_format("UPDATE tp_pattern SET stock = stock - '{0}' WHERE pid = '{1}' AND is_delete = 0 AND stock - '{0}' > 0", $stock, $pid);
        }
        $this->Pattern_model->execute($sql);
    }

    /**
     * 方法名 dealStock
     *
     * @Name dealStock
     * @Desc 获取库存
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2022-02-17 14:11:28
     * @return      [type]                   [description]
     */
    private function getStock($pid) {
        $this->db->trans_start();
        $sql = sql_format("SELECT stock FROM tp_pattern WHERE pid = {0} FOR UPDATE", $pid);
        $this->db->trans_complete();
        $info = $this->Pattern_model->single($sql);

        empty($info) && exit(retJson(ErrCode::ErrCode_10801));
        return $info->stock;

        // $info = $this->Pattern_model->get_single(['pid' => $pid]);
        // return $info['stock'];
    }




    /**
     * 方法名 orderStatus
     *
     * @Name orderStatus
     * @Desc 订单状态
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      DJC
     * @Date 2022-02-17 14:11:28
     * @return      [type]                   [description]
     */
    public function orderStatus($param) {
        if (!isset($param['id'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }

        $order = $this->Order_model->get_single(array('id' => $param['id']));

        $orderC = $this->getRole($order['creator']);

        if ($order['status'] == SubmitStatus::zero[TPEnum::Value] && $this->role->role_id != $orderC->role_id) exit(retJson(ErrCode::ErrCode_10701));
        if ($order['status'] == SubmitStatus::second[TPEnum::Value] && ($this->role->role_id != WebRole::BusinessS[TPEnum::Value] && $this->role->role_id != WebRole::manager[TPEnum::Value])) exit(retJson(ErrCode::ErrCode_10701));

        $num = 0;
        if ($orderC->role_id == WebRole::Customer[TPEnum::Value] ) {
            if ($order['status'] == SubmitStatus::First[TPEnum::Value] && $this->role->role_id != WebRole::Salesman[TPEnum::Value]) exit(retJson(ErrCode::ErrCode_10701));
            $num = 1;
        }

        if ($orderC->role_id != WebRole::Customer[TPEnum::Value]) {
            if ($order['status'] == SubmitStatus::zero[TPEnum::Value]) {
                $num = 2;
            } else {
                $num = 1;
            }
        }

        if ($order['status'] == SubmitStatus::third[TPEnum::Value]) exit(retJson(ErrCode::ErrCode_10702));


        $sql = sql_format("UPDATE tp_order SET `status` = `status`+{2} where id = '{0}' and `status` < {1}", $param['id'], SubmitStatus::third[TPEnum::Value], $num);

        $this->Order_model->execute($sql);


        return true;
    }

    /**
     * 方法名 BackSubmit
     *
     * @Name BackSubmit
     * @Desc 订单驳回
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      DJC
     * @Date 2022-02-17 17:12:53
     * @return      [type]                   [description]
     */
    public function BackSubmit($param) {
        if (!isset($param['oid'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }

        $order = $this->Order_model->get_single(array('id' => $param['oid']));


        if ($order['status'] == SubmitStatus::third[TPEnum::Value]) exit(retJson(ErrCode::ErrCode_10702));
        if ($order['status'] == SubmitStatus::second[TPEnum::Value] && ($this->role->role_id != WebRole::BusinessS[TPEnum::Value] && $this->role->role_id != WebRole::manager[TPEnum::Value] )) exit(retJson(ErrCode::ErrCode_10701));
        if ($order['status'] == SubmitStatus::First[TPEnum::Value] && $this->role->role_id != WebRole::Salesman[TPEnum::Value]) exit(retJson(ErrCode::ErrCode_10701));
        if ($order['status'] == SubmitStatus::zero[TPEnum::Value]) exit(retJson(ErrCode::ErrCode_10703));


        $orderC = $this->getRole($order['creator']);

        if (($orderC->role_id == WebRole::BusinessS[TPEnum::Value] || $orderC->role_id == WebRole::Salesman[TPEnum::Value] ||$orderC->role_id == WebRole::manager[TPEnum::Value]) && $order['status'] == SubmitStatus::second[TPEnum::Value]) {
            $num = 2;
        } else {
            $num = 1;
        }

        $sql = sql_format("UPDATE tp_order SET `status` = `status`-{1} where id = '{0}' ", $param['oid'], $num);

        $this->Order_model->execute($sql);


        return true;
    }



    /**
     * 方法名 abolishOrder
     *
     * @Name abolishOrder
     * @Desc 废除订单
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      DJC
     * @Date 2022-02-17 18:14:30
     * @return      [type]                   [description]
     */
    public function abolishOrder($param) {
        if (!isset($param['id'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }

        $order = $this->Order_model->get_single(array('id' => $param['id']));

        if (empty($order)) exit(retJson(ErrCode::ErrCode_10704));

        $data = array(
            'abolish' => $order['abolish'] == DeleteState::UnDelete[TPEnum::Value] ?  DeleteState::Delete[TPEnum::Value] : DeleteState::UnDelete[TPEnum::Value]
        );

        $this->Order_model->update($data, array('id' => $param['id']));

        return true;
    }



    /**
     * 方法名 editOrder
     *
     * @Name editOrder
     * @Desc 修改订单
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      DJC
     * @Date 2022-02-18 18:10:22
     * @return      [type]                   [description]
     */
    public function editOrder($param) {
        if (!isset($param['oid'], $param['pid'], $param['num'], $param['delivery_date'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }


        $conarr['join'] = array(
            ['tp_order o', 'o.id = tp_order_goods.oid'],
            ['tp_pattern p', 'p.pid =  tp_order_goods.pid']
        );
        $conarr['where'] = sql_format(" tp_order_goods.id = '{0}'", $param['id']);

        $conarr['items'] = "tp_order_goods.*,p.stock,p.no,o.update_info,o.status,o.abolish,o.creator";
        $order_goods = $this->Order_goods_model->get_single_full($conarr);
        $orderC = $this->getRole($order_goods['creator']);




        $order_goods['abolish'] == DeleteState::Delete[TPEnum::Value] && exit(retJson(ErrCode::ErrCode_10802));


        if ($order_goods['status'] == SubmitStatus::zero[TPEnum::Value] && $this->role->role_id != $orderC->role_id) exit(retJson(ErrCode::ErrCode_10701));
        if ($order_goods['status'] == SubmitStatus::First[TPEnum::Value] && $this->role->role_id != WebRole::Salesman[TPEnum::Value]) exit(retJson(ErrCode::ErrCode_10701));
        if ($order_goods['status'] == SubmitStatus::second[TPEnum::Value] && ($this->role->role_id != WebRole::BusinessS[TPEnum::Value] && $this->role->role_id != WebRole::manager[TPEnum::Value])) exit(retJson(ErrCode::ErrCode_10701));
        if ($order_goods['status'] == SubmitStatus::third[TPEnum::Value]) exit(retJson(ErrCode::ErrCode_10701));

        //判断订单数量
        if ($param['num'] != $order_goods['num']) {
            $order_goods['update_info'] .= $this->role->username . "在" . date('Y-m-d H:i:s') . "把花型编号为" . $order_goods['no'] . "的下单数量改为" . $param['num'] . ";";

        //     //减少或增加的值
        //     $num = abs($order_goods['num'] - $param['num']);
        //     $type = false;
        //     if ($param['num'] >  $order_goods['num']) {
        //         if ($num > $order_goods['stock']) exit(retJson(ErrCode::ErrCode_10803));
        //         $type = true;
        //     }
        //     $this->dealStock($order_goods['pid'], $num, $type);
        }
        //判断交期
        if ($param['delivery_date'] != $order_goods['delivery_date']) {
            $order_goods['update_info'] .= $this->role->username . "在" . date('Y-m-d H:i:s') . "把花型编号为" . $order_goods['no'] . "的要求交期改为" . $param['delivery_date'] . ";";
        }


        $goods = array(
            'num' => $param['num'],
            'delivery_date' => $param['delivery_date'],
            'remark' => $param['remark'],
			'color' => $param['color']
        );

        $this->Order_goods_model->update($goods, array('id' => $order_goods['id']));

        $this->Order_model->update(['update_info' => $order_goods['update_info']], ['id' => $order_goods['oid']]);

        return true;
        // $order_goods = $this->
    }




    /**
     * 方法名 OrderIsBuy
     *
    * @Name OrderIsBuy
    * @Desc 方法描述
    * @OpenFlag 是否开放,1开放0权限控制
    * @Author      DJC
    * @Date 2022-02-28 15:55:53
    * @return      [type]                   [description]
    */
    public function OrderIsBuy($param){
        if(!isset($param['account_id'],$param['pids'])){
            exit(retJson(ErrCode::ErrCode_10002));
        }
        if($this->role->role_id == WebRole::Customer[TPEnum::Value]){
            $account_id = $this->role->id;
        }else{

            if(empty($param['account_id'])){
                return [];
            }

            $account_id = $param['account_id'];
        }

        if(empty($param['pids'])){
            return [];
        }

        $parr = $this -> client->checkBuy($param['pids'],$account_id);
        return $parr;
    }
}
